package frontend.admin;

import backend.DiseaseItem;
import backend.GeneralDatabase;
import backend.PermissionDenied;
import service.DatabaseManager;

import javax.swing.*;
import java.awt.*;

public class AddDiseaseDialog extends JDialog {
    public interface AddDiseaseCallback {
        void onDiseaseAdded(DiseaseItem disease);
    }

    public AddDiseaseDialog(JFrame owner, DiseaseItem disease, AddDiseaseCallback callback) {
        super(owner, "添加疾病", true);
        setSize(500, 400);
        setLocationRelativeTo(owner);
        setLayout(new BorderLayout(10, 10));

        JPanel formPanel = new JPanel(new GridLayout(9, 2, 10, 10));
        formPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 10, 15));

        JTextField nameField = new JTextField(disease.Cause());
        JTextField symptomField = new JTextField(disease.Symptom());
        JTextField organField = new JTextField(disease.Organ());
        JTextField medicationField = new JTextField(disease.Medication());
        JTextField reliefMethodField = new JTextField(disease.Reliefmethod());
        JTextField dietField = new JTextField(disease.Diet());
        JTextField impactField = new JTextField(disease.Impact());
        JTextField urgencyField = new JTextField(disease.Urgency());
        JTextField departmentField = new JTextField(disease.Department());

        formPanel.add(new JLabel("疾病名称："));
        formPanel.add(nameField);
        formPanel.add(new JLabel("症状："));
        formPanel.add(symptomField);
        formPanel.add(new JLabel("发生部位："));
        formPanel.add(organField);
        formPanel.add(new JLabel("药物治疗："));
        formPanel.add(medicationField);
        formPanel.add(new JLabel("缓解方式："));
        formPanel.add(reliefMethodField);
        formPanel.add(new JLabel("饮食建议："));
        formPanel.add(dietField);
        formPanel.add(new JLabel("影响程度："));
        formPanel.add(impactField);
        formPanel.add(new JLabel("紧急程度："));
        formPanel.add(urgencyField);
        formPanel.add(new JLabel("科室："));
        formPanel.add(departmentField);

        add(formPanel, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
        JButton saveBtn = new JButton("保存");
        JButton cancelBtn = new JButton("取消");

        saveBtn.setBackground(new Color(70, 130, 180));
        saveBtn.setForeground(Color.WHITE);
        saveBtn.setFocusPainted(false);

        saveBtn.addActionListener(e -> {
            String name = nameField.getText().trim();
            String symptom = symptomField.getText().trim();
            String organ = organField.getText().trim();
            String medication = medicationField.getText().trim();
            String reliefMethod = reliefMethodField.getText().trim();
            String diet = dietField.getText().trim();
            String impact = impactField.getText().trim();
            String urgency = urgencyField.getText().trim();
            String department = departmentField.getText().trim();

            if (!name.isEmpty() && !symptom.isEmpty() && !organ.isEmpty() &&
                    !medication.isEmpty() && !reliefMethod.isEmpty() && !diet.isEmpty() &&
                    !impact.isEmpty() && !urgency.isEmpty()&& !department.isEmpty()) {

                DiseaseItem newItem = new DiseaseItem()
                        .Cause(name)
                        .Symptom(symptom)
                        .Organ(organ)
                        .Medication(medication)
                        .Reliefmethod(reliefMethod)
                        .Diet(diet)
                        .Impact(impact)
                        .Urgency(urgency);
                        newItem.Department(department);

                try {
                    GeneralDatabase db = DatabaseManager.getInstance();
                    String result = db.tryItemadd("admin", newItem); // 假设管理员用户名为 admin
                    if ("SUCCESS".equals(result)) {
                        callback.onDiseaseAdded(newItem);
                        dispose();
                    } else {
                        JOptionPane.showMessageDialog(this, "添加疾病失败: " + result);
                    }
                } catch (PermissionDenied ex) {
                    JOptionPane.showMessageDialog(this, "权限不足，无法添加疾病");
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(this, "添加疾病失败: " + ex.getMessage());
                }
            } else {
                JOptionPane.showMessageDialog(this, "请填写所有字段");
            }
        });

        cancelBtn.addActionListener(e -> dispose());

        buttonPanel.add(saveBtn);
        buttonPanel.add(cancelBtn);
        add(buttonPanel, BorderLayout.SOUTH);
    }
}
